package com.example.aaa_viewpager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.example.aaa_viewpager.entity.User;
import com.example.aaa_viewpager.service.UserDAO;
import com.example.bb_serch.R;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;

import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Im;

import android.speech.RecognizerIntent;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import android.widget.ListView;


public class N_Activity extends Activity 
{
	//UI
	private ListView listView;
	private EditText editText;
	private ImageButton search_btn;
	private ImageButton voice_btn;
	
	
	 //資料庫
	private UserDAO userDao;
	private ArrayList<HashMap> bb_list;
	private Cursor cursor;
	
	
	///listView列表的id 取得後傳給BB_details使用
	private int listView_id; 
	
	//從fragment1取得的資料庫位置
	private int position;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_n);
        initView();
        initHandler();
        
    }
   
	private void initHandler() 
	{
		// TODO Auto-generated method stub
		listView.setOnItemClickListener(new ListView.OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) 
			{
				//String selectedText = ((TextView) view).getText().toString();
				
				///所選取的民宿之ID 傳到BB_details.class
				listView_id = (int) listView.getItemIdAtPosition(position);
				
				Intent it = new Intent();
				Bundle bundle = new Bundle();
				bundle.putInt("BB_POSITION", listView_id);
				
				//這裡的123 是用來讓BB_details.getData()判斷使用
				//bundle.putInt("BB_VALUES", 123);
				it.putExtras(bundle);
				it.setClass(N_Activity.this, BB_details.class);
				N_Activity.this.startActivity(it);

				
			}
		});
		
		//語音功能...如果無網路服務無法使用
		voice_btn.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				try
				{
					Intent it = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
					it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
					it.putExtra(RecognizerIntent.EXTRA_PROMPT, "請說出關鍵字");
					//這裡的"1"是為了跟onActivityResult(int requestCode, int resultCode, Intent data)溝通
					startActivityForResult(it,1);
				}
				catch(Exception ex)
				{
					Toast.makeText(N_Activity.this, "無法使用語音", Toast.LENGTH_SHORT).show();
				}
				
			}
			
		});
		
		
		//使用者輸入關鍵字後，按下搜尋鍵..list呈現搜尋後的結果
		search_btn.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				//取得使用輸入的內容
				String inputStr = editText.getText().toString();
				
				//建立一個ArrayList<HashMap>物件
				ArrayList<HashMap> list = new ArrayList<HashMap>();
				
				//使用mathod:searchResult(使用者輸入內容，資料庫位置)取得的值丟入list
				//再利用自定義的adapter(baseadapter類別)丟進listView佈屬
				list = userDao.searchResult(inputStr, position);
				MyAdapter adapter1 = new MyAdapter(N_Activity.this, list);
				listView.setAdapter(adapter1);
			}
		});
		
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 1 && requestCode == RESULT_OK)
		{
			ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			if(results != null && results.size() > 0 )
			{
				editText.setText(results.get(0));
			}
		}
	}
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		// TODO Auto-generated method stub
//		super.onActivityResult(requestCode, resultCode, data);
//		if(requestCode==1 && resultCode== RESULT_OK){
//			ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//			if(results!=null && results.size()>0){
//				input.setText(results.get(0));
//			}
//		}
//	}
	private void initView() 
	{
		///標題隱藏
	    ActionBar actionBar = getActionBar();  
	    actionBar.hide();  
	    
		listView = (ListView) findViewById(R.id.listView2);
		editText = (EditText) findViewById(R.id.editText1);
		search_btn = (ImageButton) findViewById(R.id.imageButton1);
		voice_btn = (ImageButton) findViewById(R.id.imageButton2);
		
		getData();
		userDao = new UserDAO(this);
		userDao.getAll(position);
		
		
		refreshListView();
		
	}
	///從fragment1取得...點選區域對應的資料庫編號=>UserDAO.java的getAll()方法
	private void getData()
	{
		Bundle bundle = this.getIntent().getExtras();
		position = bundle.getInt("BB");
	}

	//取得列表資料
	private void refreshListView()
	{																				
		/*
		bb_list = userDao.getAll();
		listView.setAdapter(new ArrayAdapter(N_Activity.this,android.R.layout.simple_list_item_1, bb_list));
		*/
//		
//		Cursor cursor =  userDao.getAllNotesCursor();
//		String[] from = {"NAME","ADDRESS",};
//		int[] to = {R.id.name,R.id.address};
//		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item_n, cursor, from, to, 0);
//		listView.setAdapter(adapter);
		
		//baseAdapter
		bb_list = userDao.getAll();
		MyAdapter adapter = new MyAdapter(this, bb_list);
		listView.setAdapter(adapter);
	}
	
	
		
	
	
    
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sec, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
